/**
 * 路由处理模块
 */

const User_module = require('../db_modules/users_module');
const config = require('../utils/config');
const jwtUtil = require('../utils/jwtUtils');

module.exports = class User_handler extends User_module {
  /**
   * 用户登录操作
   * @param {string} username
   * @param {string} password
   * @param {number} type
   * @return {Object} {userLoginData, jwt_token}
   */
  static async userLogin(req, res) {
    console.log('login api is requested');
    const body = req.body;
    const username = body.username;
    const password = body.password;
    const type = body.type;
    const loginData = await User_module.getUser(username, password, type);

    if (loginData.length !== 0) {
      const token = jwtUtil.sign({
        id: loginData[0].id,
        username: loginData[0].username,
        password: '',
        type: loginData[0].type
      },
        config.secretkey, config.expiresIn);
      res.send({ state: 1, loginData, token });
    } else {
      res.cc('用户信息错误！！！');
    }
  }

  /**
   * 通过用户 id 获取用户信息
   * @param {string} uid
   * @return {object} userData
   */
  static async getUserById(req, res) {
    console.log('getUserById api is requested');
    const body = req.body;
    // const id = Number(body.id);
    const userData = await User_module.getUserDataById(body.id);
    if (userData.length >= 0) {
      res.send({ state: 1, userData });
    } else {
      res.cc('查询失败')
    }
  }

  /**
   * 通过 token 获取用户数据
   * @param {string} token
   * @return {object} userData
   */
  static async getUserByToken(req, res) {
    console.log('getUserByToken api is requested');
    const token = req.headers.authorization;
    const userData = await jwtUtil.verifysync(token, config.secretkey);
    if (userData.length >= 0) {
      res.send({ state: 1, userData });
    } else {
      res.cc('查询失败')
    }
  }

  /**
   * 更新头像
   * @param {string} newAvatar  头像文件名称，暂从本地固定
   * @param {number} id
   * @return {object} results
   */
  static async upDateAvatar(req, res) {
    console.log('upDateAvatar api is requested');
    const newAvatar = req.body.newAvatar;
    const id = req.body.id;
    const results = await User_module.upAvatar(newAvatar, id);
    if (results.affectedRows !== 1) {
      res.cc('更新头像失败');
    } else {
      res.send({ message: '更新头像成功', state: 1 });
    }
  }

  /**
   * 修改密码
   * @param {string} oldpwd 旧密码
   * @param {string} newpwd  新密码
   * @param {number} id   用户id
   * @returns {object} results
   */
  static async upDatePwd(req, res) {
    console.log('upDatePwd api is requested');
    const body = req.body;
    const oldpwd = body.oldpwd;
    const newpwd = body.newpwd;
    const id = body.id;
    const results = await User_module.upPassword(oldpwd, newpwd, id);
    if (results.affectedRows !== 1) {
      res.cc('修改密码失败');
    } else {
      res.send({ message: '修改密码成功', state: 1 });
    }
  }

  /**
   * 根据用户类型获取用户列表
   * @param {number} type 用户类型
   * @returns {object} results 对应用户列表
   */
  static async getUserList(req, res) {
    console.log('getUserList api is requested');
    const body = req.body;
    if (body.type < 1 || body.type > 3 || body.type % 1 !== 0) {
      res.cc('invalid params')
    } else {
      const results = await User_module.getlistByType(body.type);
      if (results.length >= 0) {
        res.send({ state: 1, results });
      } else {
        res.cc('查询失败');
      }
    }
  }

  /**
   * 根据用户类型获取用户列表（分页）
   * @param {number} type  用户类型
   * @param {number} curPage   当前页
   * @param {number} pageNum    当前页的数据数量
   * @return {object} results   对应用户列表
   */
  static async getUserListPage(req, res) {
    console.log('getUserListtPage api is requested');
    const body = req.body;
    if (body.type < 1 || body.type > 3 || body.type % 1 !== 0) {
      res.cc('invalid params');
    } else {
      const results = await User_module.getlistByTypePage(body.type, body.curPage, body.pageNum);
      if (results.length >= 0) {
        res.send({ state: 1, results });
      } else {
        res.cc('查询失败！')
      }
    }
  }

  /**
   * 获取通知（分页）
   * @param {number} curPage
   * @param {number} pageNum
   * @return {object} results
   */
  static async getUserNotices(req, res) {
    console.log('getUserNotices api is requested');
    const body = req.body;
    const results = await User_module.getNotices(body.curPage, body.pageNum);
    if (results.length >= 0) {
      res.send({ state: 1, results });
    } else {
      res.cc('查询失败！')
    }
  }
}
